const { qq_request } = require("../../../util/qq_request");
const APIError = require("../../../middlewares/rest").APIError;

let playlist = async (ctx) => {
    
    if (ctx.request.method === 'GET') {
        var pageNo = ctx.request.query.offset || '0';
        var limit = ctx.request.query.limit || '20';
        var categoryId = ctx.request.query.categoryId || '10000000';
        // console.log(typeof ctx.request.query.limit, limit);
    } else if (ctx.request.method === 'POST') {
        var pageNo = ctx.request.body.offset || '0';
        var limit = ctx.request.body.limit || '20';
        var categoryId = ctx.request.body.categoryId || '10000000';
    }

    if (pageNo.length === 0) {
        throw new APIError("QQ_playlist", "page not found");
    }
    
     let result = await qq_request(`https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_by_tag.fcg`, {
        inCharset: 'utf8',
        outCharset: 'utf-8',
        sortId: 3,
        categoryId: categoryId,
        sin: limit * (pageNo - 1),
        ein: pageNo * limit - 1,
        format: 'json'
    });
    
    ctx.rest(result.data);
}

let playlist_Category = async (ctx) => {

     let result = await qq_request(`https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_tag_conf.fcg?format=json&inCharset=utf8&outCharset=utf-8`);
    
    ctx.rest(result.data);
}


let playlist_Info = async (ctx) => {
    if (ctx.request.method === 'GET') {
        var pid = ctx.request.query.pid || '';
        // console.log(typeof ctx.request.query.limit, limit);
    } else if (ctx.request.method === 'POST') {
        var pid = ctx.request.body.pid || '';
    }

    if (pid.length === 0) {
        throw new APIError("QQ_playlist", "pid not found");
    }

    let result = await qq_request(`http://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg`, {
        type: 1,
        utf8: 1,
        disstid: pid.trim(), // 歌单的id
        loginUin: 0,
        format: 'json'
    });
    
    ctx.rest(result.data);
}

module.exports = {
    playlist_Info,
    playlist,
    playlist_Category
}